package com.yc.jpyy.question.service.imp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.yc.jpyy.base.service.BaseService;
import com.yc.jpyy.question.dao.QuestionDao;
import com.yc.jpyy.question.service.QuestionService;
import com.yc.jpyy.utils.Tools;


@Service
public class QuestionServiceImpl extends BaseService implements QuestionService {
 

@Autowired
private QuestionDao questionDao;

@Override
public Map<String, Object> addQuestion(Map<String,Object> paramsMap)
		throws Exception {
	Map<String,Object> resultMap = new HashMap<>();
	//不是自增主键，在插入的时候获取不到
	 UUID uuid = UUID.randomUUID();
	 paramsMap.put("uuid", uuid.toString());
	//新增主表
	int count = questionDao.addQuestion(paramsMap);
	if(count > 0){
		//获取到子表数据
		List<Map<String,Object>> questionItemList = new ArrayList<Map<String,Object>>();
		//获取JSON对象中的JSON对象数组
		JSONArray jsonArray = JSONArray.fromObject(paramsMap.get("questionitem"));
		int jsonArraySize = jsonArray.size();//获取数组长度
		for(int i = 0; i < jsonArraySize; i++ ){
			//JSON对象转JSON字符串
			String str = jsonArray.getJSONObject(i).toString();
			//JSON字符串转map
			Map<String,Object> paramsMaps = Tools.JsonToMap(str);
			paramsMaps.put("questionid",uuid.toString());
			questionItemList.add(paramsMaps);
		}
		//批量新增子表
		int counts = questionDao.addQuestionItem(questionItemList);
		if(counts > 0){
			resultMap.put("returnCode", 1);
			resultMap.put("returnMsg", "新增成功");	
		}else{
			throw new Exception();
		}
	}else{
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "新增失败");
	}
	return resultMap;
}

@Override
public Map<String, Object> selectQuestionByPage(Map<String, Object> paramsMap)
		throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
	int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
	paramsMap.put("pagestart", (pagenum-1) * pagesize);
	List<Map<String,Object>> QuestionList = questionDao.selectQuestionByPage(paramsMap);
	int QuestionCounts = questionDao.selectQuestionCount(paramsMap);
	if(QuestionList != null && QuestionList.size() > 0){
		resultMap.put("returnCode", 1);
		resultMap.put("ListData",QuestionList);
		resultMap.put("TotalCount",QuestionCounts);
		resultMap.put("TotalPage",QuestionCounts % pagesize >0 ? QuestionCounts/pagesize + 1 : QuestionCounts/pagesize);
		resultMap.put("returnMsg", "操作成功");
	}else{
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "系统无数据");
	}
	return resultMap;
}

@Override
public Map<String, Object> selectQuestionItemByPage(Map<String, Object> paramsMap)
		throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
	int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
	paramsMap.put("pagestart", (pagenum-1) * pagesize);
	List<Map<String,Object>> QuestionItemList = questionDao.selectQuestionByPage(paramsMap);
	int QuestionItemCounts = questionDao.selectQuestionCount(paramsMap);
	if(QuestionItemList != null && QuestionItemList.size() > 0){
		//List<Object> QuestionItems= QuestionItemList.stream().map(map->map.put("questionItem", "")).collect(Collectors.toList());
		for (Map<String, Object> maps : QuestionItemList) {
			paramsMap.put("ID", maps.get("ID").toString());
			maps.put("questionItem",questionDao.getQuestionItems(paramsMap));
		}
		resultMap.put("returnCode", 1);
		resultMap.put("ListData",QuestionItemList);
		resultMap.put("TotalCount",QuestionItemCounts);
		resultMap.put("TotalPage",QuestionItemCounts % pagesize >0 ? QuestionItemCounts/pagesize + 1 : QuestionItemCounts/pagesize);
		resultMap.put("returnMsg", "操作成功");
	}else{
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "系统无数据");
	}
	return resultMap;
}


@Override
public Map<String, Object> updateQuestion(Map<String,Object> paramsMap)
		throws Exception {
	Map<String,Object> resultMap = new HashMap<>();
	//先更新主表
	int i = questionDao.updateQuestion(paramsMap);
	if(i > 0){
		//获取子表信息，并转换为json数组
		JSONArray jsonArray = JSONArray.fromObject(paramsMap.get("questionitem"));
		List<Map<String,Object>> updateList = new ArrayList<Map<String,Object>>();
		//修改子表的时候有可能子表的记录会变多，以子表的Id主键为0代表是新增的记录
		List<Map<String,Object>> addList = new ArrayList<Map<String,Object>>();
		for(int k = 0 ; k < jsonArray.size() ;k ++){
			//JSON对象转JSON字符串
			String str = jsonArray.getJSONObject(k).toString();
			//JSON字符串转map
			Map<String,Object> paramsMaps = Tools.JsonToMap(str);
			paramsMaps.put("questionid",paramsMap.get("id"));
			if(paramsMaps.get("id").toString().equals("0")){
				addList.add(paramsMaps);
			}else{
				updateList.add(paramsMaps);
			}
		}
		//批量更新子表
		int j = questionDao.updateQuestionItem(updateList);
		if(j > 0){
			if(addList != null && addList.size() > 0){
				//批量新增子表
				int count = questionDao.addQuestionItem(updateList);
				if(count > 0){
					resultMap.put("returnCode", 1);
					resultMap.put("returnMsg", "修改成功");
				}else{
					throw new Exception();
				}
			}else{
				resultMap.put("returnCode", 1);
				resultMap.put("returnMsg", "修改成功");
			}
		}else{
			throw new Exception();
		}
	}else{
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "修改失败");
	}
	return resultMap;
}

@Override
public Map<String, Object> delQuestion(Map<String, Object> paramsMap)
		throws Exception {
	Map<String,Object> resultMap = new HashMap<>();
	paramsMap.put("ids", paramsMap.get("ids").toString().split(","));
	//删除主表
	int count = questionDao.delQuestion(paramsMap);
	if(count > 0){
		//删除对应子表
		int k = questionDao.delQuestionItem(paramsMap);
		if(k > 0){
			resultMap.put("returnCode", 1);
			resultMap.put("returnMsg", "删除成功");
		}else{
			throw new Exception();
		}
	}else{
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "删除失败");
	}
	return resultMap;
}

@Override
public Map<String, Object> isOnQuestionsName(Map<String, Object> paramsMap)
		throws Exception {
	Map<String,Object> resultMap = new HashMap<>();
	//查询
	int count = questionDao.isOnQuestionsName(paramsMap);
	if(count >= 0){
		resultMap.put("returnCode", 1);
		resultMap.put("returnMsg", "查询成功");
		resultMap.put("ListData", count);	
	}else{
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "查询失败");
	}
	return resultMap;
}

@Override
public Map<String, Object> selectQuestionItemByID(Map<String, Object> paramsMap)
		throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	List<Map<String,Object>> questionItem = questionDao.selectQuestionItemByID(paramsMap);
	if(questionItem != null && questionItem.size() > 0){
		for (Map<String, Object> maps : questionItem) {
			paramsMap.put("ID", maps.get("ID").toString());
			maps.put("questionItem",questionDao.getQuestionItems(paramsMap));
		}			
		resultMap.put("returnCode", 1);
		resultMap.put("ListData",questionItem);
		resultMap.put("returnMsg", "操作成功");
	}else{
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "系统无数据");
	}
	return resultMap;
}

}
